

const mongoose = require('mongoose');

var express = require('express')
// 注意这里的声明
var favorite = express()
// const controller= require("../../controller/controllerenter")

const {createResponse}= require("../../util/createResponse/index");
const {businessCodes}= require("../../code/businessCodes/index");
const { favoriteService } = require('../../service/impl/favoriteService/favoriteService');
// const { favorite_bean } = require('../../beans/impl/favorite_bean/index');


favorite.post('/post/favorite/setAndUnset', async (req,res) => {
  let result
  try {
    let conditions = req.body.conditions
    let updates  = {
        "favo" :req.body.updates.favo
    }
    let options= {
        "upsert":true,
        "new":true
    }
    result =  await favoriteService.getIns().findOneAndUpdate(conditions,updates,options)
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  favorite.post('/post/favorite/delete', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      result =  await favoriteService.getIns().findByIdAndDelete(id)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })
  
  favorite.post('/post/favorite/update', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      let updates = req.body.updates
      result =  await favoriteService.getIns().findByIdAndUpdate(id,updates)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })
  
  favorite.post('/post/favorite/query', async (req,res) => {
    let result
    try {
      result =  await favoriteService.getIns().find()
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })
  
  module.exports= favorite